package com.euc.springbootframework.util;

import org.apache.commons.io.FilenameUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class FileUtils {
    public static void createDir(String path, boolean isFile) {
        createDir(new File(path), isFile);
    }

    public static List<File> getAllFiles(String path) {
        List<File> files = new ArrayList<File>();
        getAllFiles(path, files);
        return files;
    }

    public static void getAllFiles(String path, List<File> fileList) {
        File file = new File(path);
        if (file.exists()) {
            if (file.isDirectory()) {
                fileList.add(file);
                String[] files = file.list();
                for (String filePath : files) {
                    getAllFiles(path + File.separator + filePath, fileList);
                }
            } else {
                fileList.add(file);
            }
        }
    }

    public static void createDir(File file, boolean isFile) {
        if (isFile) {
            if (file.getParent() != null && file.getParent().length() != 0) {
                createDir(file.getParent(), false);
            }
        } else {
            if (!file.exists()) {
                file.mkdirs();
            }
        }
    }

    public static List<File> getFilesOfDir(String path) {
        return getFilesOfDir(new File(path));
    }

    public static List<File> getFilesOfDir(File directorie) {
        List<File> fileList = new ArrayList<File>();
        File[] files = directorie.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                fileList.addAll(getFilesOfDir(file));
            } else {
                fileList.add(file);
            }
        }
        return fileList;
    }

    public static void copy(String srcFilePath, String desFilePath) {
        File srcFile = new File(srcFilePath);
        File desFile = new File(desFilePath);
        copy(srcFile, desFile);
    }

    public static String copy(String srcFilePath, String desFilePath, boolean isIncludeDir) {
        File srcFile = new File(srcFilePath);
        File desFile = null;
        if (!srcFile.exists()) {
            return null;
        }
        if (isIncludeDir || srcFile.isFile()) {
            desFile = new File(desFilePath, srcFilePath.substring(srcFilePath.lastIndexOf(File.separator)));
        } else {
            desFile = new File(desFilePath);
        }
        copy(srcFile, desFile);
        return desFile.getPath();
    }

    public static void simpleCopy(File srcFile, File desFile) {
        if (srcFile.isDirectory()) {
            desFile.delete();
            desFile.mkdir();
        } else {
            if (desFile.exists()) {
                desFile.delete();
            }
            createDir(desFile, true);
            try {
                java.io.FileInputStream fosfrom = new java.io.FileInputStream(srcFile);
                java.io.FileOutputStream fosto = new FileOutputStream(desFile);
                byte bt[] = new byte[1024];
                int c;
                while ((c = fosfrom.read(bt)) > 0) {
                    fosto.write(bt, 0, c);
                }
                fosfrom.close();
                fosto.close();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    public static void simpleCopy(String srcFilePath, String desFilePath) {
        File srcFile = new File(srcFilePath);
        File desFile = new File(desFilePath);
        simpleCopy(srcFile, desFile);
    }

    public static void copy(File srcFile, File tarpath) {
        if (srcFile.isDirectory()) {
            createDir(tarpath, false);
            File[] files = srcFile.listFiles();
            for (int i = 0; i < files.length; i++) {
                copy(files[i], new File(tarpath + "/" + files[i].getName()));
            }
        } else {
            if (!tarpath.getParentFile().exists()) {
                tarpath.getParentFile().mkdirs();
            }
            if (tarpath.exists()) {
                tarpath.delete();
            }
            createDir(tarpath, true);
            try {
                java.io.FileInputStream fosfrom = new java.io.FileInputStream(srcFile);
                java.io.FileOutputStream fosto = new FileOutputStream(tarpath);
                byte bt[] = new byte[1024];
                int c;
                while ((c = fosfrom.read(bt)) > 0) {
                    fosto.write(bt, 0, c);
                }
                fosfrom.close();
                fosto.close();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    public static void delete(String filePath) {
        File file = new File(filePath);
        delete(file);
    }
    public static void delete(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                File[] subFiles = file.listFiles();
                if (subFiles != null) {
                    for (int i = 0; i < subFiles.length; i++) {
                        File subFile = subFiles[i];
                        delete(subFile);
                    }
                }
            }
            file.delete();
        }
    }

    /**
     * 文件读取缓冲区大小
     */
    private static final int CACHE_SIZE = 1024;

    /**
     * <p>
     * 压缩文件
     * </p>
     *
     * @param sourceFolder 压缩文件夹
     * @param zipFilePath 压缩文件输出路径
     * @throws Exception
     */
    public static void zip(String sourceFolder, String zipFilePath) throws Exception {
        OutputStream out = new FileOutputStream(zipFilePath);
        BufferedOutputStream bos = new BufferedOutputStream(out);
        ZipOutputStream zos = new ZipOutputStream(bos);
        File file = new File(sourceFolder);
        String basePath = null;
        if (file.isDirectory()) {
            basePath = file.getPath();
        } else {
            basePath = file.getParent();
        }
        zipFile(file, basePath, zos);
        zos.closeEntry();
        zos.close();
        bos.close();
        out.close();
    }

    /**
     * <p>
     * 递归压缩文件
     * </p>
     *
     * @param parentFile
     * @param basePath
     * @param zos
     * @throws Exception
     */
    private static void zipFile(File parentFile, String basePath, ZipOutputStream zos) throws Exception {
        File[] files = new File[0];
        if (parentFile.isDirectory()) {
            files = parentFile.listFiles();
        } else {
            files = new File[1];
            files[0] = parentFile;
        }
        String pathName;
        InputStream is;
        BufferedInputStream bis;
        byte[] cache = new byte[CACHE_SIZE];
        for (File file : files) {
            if (file.isDirectory()) {
                pathName = file.getPath().substring(basePath.length() + 1) + "/";
                zos.putNextEntry(new ZipEntry(pathName));
                zipFile(file, basePath, zos);
            } else {
                pathName = file.getPath().substring(basePath.length() + 1);
                is = new FileInputStream(file);
                bis = new BufferedInputStream(is);
                zos.putNextEntry(new ZipEntry(pathName));
                int nRead = 0;
                while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {
                    zos.write(cache, 0, nRead);
                }
                bis.close();
                is.close();
            }
        }
    }

    /**
     * <p>
     * 解压压缩包
     * </p>
     *
     * @param zipFilePath 压缩文件路径
     * @param destDir 压缩包释放目录
     * @throws Exception
     */
    public static void unZip(String zipFilePath, String destDir) throws Exception {
        ZipFile zipFile = new ZipFile(zipFilePath);
        Enumeration<?> emu = zipFile.entries();
        BufferedInputStream bis;
        FileOutputStream fos;
        BufferedOutputStream bos;
        File file, parentFile;
        ZipEntry entry;
        byte[] cache = new byte[CACHE_SIZE];
        while (emu.hasMoreElements()) {
            entry = (ZipEntry) emu.nextElement();
            if (entry.isDirectory()) {
                new File(destDir + entry.getName()).mkdirs();
                continue;
            }
            bis = new BufferedInputStream(zipFile.getInputStream(entry));
            file = new File(destDir + entry.getName());
            parentFile = file.getParentFile();
            if (parentFile != null && (!parentFile.exists())) {
                parentFile.mkdirs();
            }
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos, CACHE_SIZE);
            int nRead = 0;
            while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {
                fos.write(cache, 0, nRead);
            }
            bos.flush();
            bos.close();
            fos.close();
            bis.close();
        }
        zipFile.close();
    }

}
